package com.example.canvasdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.canvasdemo.model.RegisterRequest
import com.example.canvasdemo.model.RegisterResponse
import com.example.canvasdemo.network.ApiService
import com.example.canvasdemo.network.RetrofitClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class RegisterActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        val etUsername: EditText = findViewById(R.id.etUsername)
        val etPassword: EditText = findViewById(R.id.etPassword)
        val etConfirmPassword: EditText = findViewById(R.id.etConfirmPassword)
        val etEmail: EditText = findViewById(R.id.etEmail)
        val etPhone: EditText = findViewById(R.id.etPhone)
        val btnRegister: Button = findViewById(R.id.btnRegister)

        btnRegister.setOnClickListener {
            val username = etUsername.text.toString().trim()
            val password = etPassword.text.toString().trim()
            val confirmPassword = etConfirmPassword.text.toString().trim()
            val email = etEmail.text.toString().trim()
            val phone = etPhone.text.toString().trim()

            // 校验字段是否为空
            if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() ||
                email.isEmpty() || phone.isEmpty()
            ) {
                showToast("请填写所有字段")
                return@setOnClickListener
            }

            // 校验密码是否一致
            if (password != confirmPassword) {
                showToast("密码不一致")
                return@setOnClickListener
            }

            // 创建 RegisterRequest 对象
            val registerRequest = RegisterRequest(username, password, email, phone)
            registerUser(registerRequest)
        }
    }

    private fun registerUser(registerRequest: RegisterRequest) {
        val apiService = RetrofitClient.instance.create(ApiService::class.java)
        apiService.registerUser(registerRequest).enqueue(object : Callback<RegisterResponse> {
            override fun onResponse(call: Call<RegisterResponse>, response: Response<RegisterResponse>) {
                if (response.isSuccessful && response.body() != null) {
                    val successMessage = response.body()?.message ?: "注册成功"
                    showToast(successMessage)
                    // 跳转至登录界面
                    val intent = Intent(this@RegisterActivity, LoginActivity::class.java)
                    startActivity(intent)
                    finish()
                } else {
                    val errorMessage = response.errorBody()?.string() ?: "注册失败，请检查输入"
                    showToast(errorMessage)
                }
            }

            override fun onFailure(call: Call<RegisterResponse>, t: Throwable) {
                showToast("网络错误: ${t.message}")
            }
        })
    }


    // 统一 Toast 方法
    private fun showToast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}





